package library.and.xps.com.step.util;

import android.content.Context;
import android.graphics.Color;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by XPS on 2017/4/25.
 */

public class MapUtils {
    //设置MapView的Style
    public static void setMapCustomFile(Context context, String PATH) {
        FileOutputStream out = null;
        InputStream inputStream = null;
        String moduleName = null;
        try {
            inputStream = context.getAssets()
                    .open("customConfigdir/" + PATH);
            byte[] b = new byte[inputStream.available()];
            inputStream.read(b);

            moduleName = context.getFilesDir().getAbsolutePath();
            File f = new File(moduleName + "/" + PATH);
            if (f.exists()) {
                f.delete();
            }
            f.createNewFile();
            out = new FileOutputStream(f);
            out.write(b);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        MapView.setCustomMapStylePath(moduleName + "/" + PATH);
    }

    public static void moveCameraTo(LatLng latLng, BaiduMap baiduMap) {
        MapStatus mMapStatus = new MapStatus.Builder().target(latLng
        ).build();
        MapStatusUpdate u = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        baiduMap.animateMapStatus(u);
    }

    //环绕百度上地总部的点
    public static final LatLng[] latlngs = new LatLng[]{
            new LatLng(40.055826, 116.307917),
            new LatLng(40.055916, 116.308455),
            new LatLng(40.055967, 116.308549),
            new LatLng(40.056014, 116.308574),
            new LatLng(40.056440, 116.308485),
            new LatLng(40.056816, 116.308352),
            new LatLng(40.057997, 116.307725),
            new LatLng(40.058022, 116.307693),
            new LatLng(40.058029, 116.307590),
            new LatLng(40.057913, 116.307119),
            new LatLng(40.057850, 116.306945),
            new LatLng(40.057756, 116.306915),
            new LatLng(40.057225, 116.307164),
            new LatLng(40.056134, 116.307546),
            new LatLng(40.055879, 116.307636),
            new LatLng(40.055826, 116.307697),
    };

    /**
     * @param milesperHour km/h
     *
     * 根据跑步速度获取对应的颜色
     * 正常6-7公里，比较快的是12-13公里，高手15-16公里 来自百度
     * */
    public static int CulColorInSpeed(double milesperHour){
        if(milesperHour>=0&&milesperHour<3){
            return Color.BLUE;
        }else if(milesperHour>3&&milesperHour<6){
            return Color.YELLOW;
        }else if(milesperHour>6&&milesperHour<12){
            return Color.RED;
        }else if(milesperHour>15){
            return Color.parseColor("#FF5D0000");
        }

        return Color.BLUE;
    }
}
